<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="输入产品名称"></el-input>
      </el-form-item>
      <el-form-item label="规格" prop="spec">
        <el-input v-model="dataForm.spec" placeholder="输入产品规格"></el-input>
      </el-form-item>

      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="输入产品价格"></el-input>
      </el-form-item>

      <el-form-item label="单位" prop="unit">
        <el-select v-model="dataForm.unit" placeholder="请选择单位">
          <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="分类" prop="category">
        <el-cascader :options="catList" v-model="dataForm.category"></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from '@/utils/validate'
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      catList: [],
      unitList: [],
      dataForm: {
        id: 0,
        name: '',
        spec: '',
        price: '',
        unit: '',
        category: []
      },
      dataRule: {
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        comfirmPassword: [
          { validator: validateComfirmPassword, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { validator: validateEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.$http.request('/depot/category/list').then(res => {
        this.catList = res.data && res.data.code === 0 ? res.data.list : []
      }).then(() => {
        this.$http.request('/depot/unit/list').then(res => {
          this.unitList = res.data && res.data.code === 0 ? res.data.list : []
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http.request(`/depot/material/info/${this.dataForm.id}`).then(res => {
              if (res.data && res.data.code === 0) {
                this.dataForm.name = res.data.list.name
                this.dataForm.spec = res.data.list.spec
                this.dataForm.price = res.data.list.price
                this.dataForm.unit = res.data.list.unit
                this.dataForm.category = res.data.list.category
                this.visible = true
              }
            })
          }
        })
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.dataForm)
        if (valid) {
          let data = {
            'id': this.dataForm.id || undefined,
            'name': this.dataForm.name,
            'spec': this.dataForm.spec,
            'price': this.dataForm.price,
            'unit': this.dataForm.unit,
            'category': this.dataForm.category
          }
          this.$http.request(`/sys/user/${!this.dataForm.id ? 'save' : 'update'}`, data, 'post').then(res => {
            if (res.data && res.data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
